package com.mmy.controller.ship;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.ship.TShipPort;
import com.mmy.domain.ship.VShipPort;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.ship.impl.TShipPortServiceImpl;

/**
 * 类的介绍：港口信息
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */

@Controller
public class ShipPortController {

	@Autowired
	TShipPortServiceImpl tShipPortServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/shipport")
	public String ToShipPort(Model model) {
		try {

			List<VShipPort> lstShipPort = tShipPortServiceImpl.findAll();
			model.addAttribute("slist", lstShipPort);


			return "webbackground/ship/shipport";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createshipport")
	public String ToCreateShipPort(Model model) {

		List<TConstZone> lstProvinceZone = constZoneServiceImpl.findAllProvince();
		model.addAttribute("plist", lstProvinceZone);
		List<TConstZone> lstCityZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("clist", lstCityZone);

		return "webbackground/ship/createshipport";
	}

	@RequestMapping(value = "/createoutshipport")
	public String ToCreateOutShipPort(Model model) {

		List<TConstZone> lstContinentZone = constZoneServiceImpl.findAllContinent();
		model.addAttribute("clist", lstContinentZone);

		List<TConstZone> lstCountryZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstCountryZone);

		return "webbackground/ship/createoutshipport";
	}

	/**
	 * 函数功能说明： 新建港口信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/subcreateshipport")
	public String CreateShipPort(TShipPort tShipPort, HttpSession session) throws Exception {
//		获取最大值
		Object oPortID = tShipPortServiceImpl.getMaxPortID();

//		 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 
		if (oPortID == null) {
			tShipPort.setPortID(1);

		} else {
			tShipPort.setPortID((int) oPortID + 1);
		}

		try {

			tShipPort.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tShipPort.setCreator(tSysUser.getUserName());
			tShipPortServiceImpl.save(tShipPort.getPortID());
			tShipPortServiceImpl.save(tShipPort);
			return "redirect:shipport";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除港口信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delshipport")
	@ResponseBody
	public String DelShipPort(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPortID = new ArrayList<>();

		for (String str : checklist) {
			lstPortID.add(Integer.parseInt(str));
		}

		tShipPortServiceImpl.delShipPort(lstPortID);

		return "webbackground/ship/shipport";
	}

	/**
	 * 函数功能说明： 修改港口信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateshipport")
	public String EnterUpdateShipPort(HttpServletRequest request, Model model) {

		int iPortID = Integer.parseInt(request.getParameter("iPortID"));

		TShipPort tShipPort = tShipPortServiceImpl.findByPortID(iPortID);
		model.addAttribute("tShipPort", tShipPort);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstConstZone);
		return "webbackground/ship/updateshipport";

	}

	@RequestMapping(value = "/updateshipport")
	public String UpdateShipPort(TShipPort tShipPort, HttpSession session) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TShipPort dtShipPort = tShipPortServiceImpl.findByPortID(tShipPort.getPortID());
//			dtShipPort.setZoneID(tShipPort.getZoneID());
			dtShipPort.setPortName(tShipPort.getPortName());
			dtShipPort.setMemo(tShipPort.getMemo());
			dtShipPort.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipPort.setReviser(tSysUser.getUserName());
			tShipPortServiceImpl.save(dtShipPort);
			return "redirect:shipport";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询港口信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchshipport")
	public String FindShipPort(HttpServletRequest request, Model model) {

		String strPortName = request.getParameter("PortName");
		if (strPortName == "") {
			List<VShipPort> lstShipPort = tShipPortServiceImpl.findAll();
			model.addAttribute("slist", lstShipPort);
			request.setAttribute("strPortName", strPortName);
			return "webbackground/ship/shipport";
		} else {
			List<VShipPort> lstShipPort = tShipPortServiceImpl.findByPortName(strPortName);
			model.addAttribute("slist", lstShipPort);
			request.setAttribute("strPortName", strPortName);
			return "webbackground/ship/shipport";
		}

	}

}
